
public class PercolationStats {

	private Percolation perc;
	private double[] percValues;
	
	public PercolationStats(int N, int T) {	 // perform T independent computational experiments on an N-by-N grid
		if (N <= 0 || T <= 0)
			throw new IllegalArgumentException();
		
		percValues = new double[T];	

		Out out = new Out("output.txt");
		out.println(N);
		int openCells;
		
		for (int x = 0; x < T; x++) {
			openCells = 0;
			perc = new Percolation(N);
			int i = StdRandom.uniform(N);
			int j = StdRandom.uniform(N);
			while (!perc.percolates()) {
				if (perc.isOpen(i+1, j+1)) {
					i = StdRandom.uniform(N);
					j = StdRandom.uniform(N);
				} else {
					perc.open(i+1, j+1);
					openCells++;
					out.println("\t" + (i+1) + "\t" + (j+1));
				}
			}
			
			percValues[x] = (double) openCells/(N*N);
		}
		
		
	}
	
	public double mean() {                     // sample mean of percolation threshold
		return StdStats.mean(percValues);
	}
	
	public double stddev() {                   // sample standard deviation of percolation threshold
		return StdStats.stddev(percValues);
	}
	
	public static void main(String[] args) {
		PercolationStats percStats = new PercolationStats(Integer.valueOf(args[0]), Integer.valueOf(args[1]));
		double mean = percStats.mean();
		double stddev = percStats.stddev();
		double sqrtN = Math.sqrt(Double.valueOf(args[0]));
		
		StdOut.println("mean=\t" + mean);
		StdOut.println("stddev=\t" + stddev);
		StdOut.println("95% confidence interval=\t" + (mean - 1.96*stddev/sqrtN) + ", " + (mean + 1.96*stddev/sqrtN));
	}

}
